<?php
include("include/config.inc.php");
if(!isset($_SESSION['s_activId']))
{
  header("location:checklogin.php");
}
$fromDate     = date('Y-m-d');
$toDate       = date('Y-m-d');
$loadArray    = array();
$truckId      = "";
$k = 0;
if(isset($_REQUEST['submitBtn']))
{
  $truckId  = $_REQUEST['truckNo'];
  $fromDate = $_REQUEST['fromYear']."-".$_REQUEST['fromMonth']."-".$_REQUEST['fromDay'];
  $toDate   = $_REQUEST['toYear']."-".$_REQUEST['toMonth']."-".$_REQUEST['toDay'];
  
  $truckTraking = "SELECT DATE_FORMAT(loadinginvoice.invoiceDate,'%d-%m-%Y') AS invoiceDate,driverName,
                          DATE_FORMAT(loadinginvoice.unloadingDate,'%d-%m-%Y') AS unloadingDate,
                       branch1.branchName AS fromBranch, branch2.branchName AS toBranch
                    FROM loadinginvoice 
                    LEFT JOIN branch AS branch1 ON branch1.branchId = loadinginvoice.fromId 
                    LEFT JOIN branch AS branch2 ON branch2.branchId = loadinginvoice.toId 
                    LEFT JOIN driver ON driver.truckId = loadinginvoice.truckId 
                    WHERE loadinginvoice.invoiceDate BETWEEN  '".$fromDate."' AND '".$toDate."'
                    AND loadinginvoice.truckId = ".$truckId;
  $truckTrakingRes = mysql_query($truckTraking);
  while($selctRow = mysql_fetch_array($truckTrakingRes))
  {
    $loadArray[$k]['invoiceDate']       = $selctRow['invoiceDate'];
    $loadArray[$k]['unloadingDate']     = $selctRow['unloadingDate'];
    $loadArray[$k]['fromBranch']        = $selctRow['fromBranch'];
    $loadArray[$k]['toBranch']          = $selctRow['toBranch'];
    $loadArray[$k]['driverName']        = $selctRow['driverName'];
    $k++;
  }
}
$i=0;
$truck = array();
$selectTruck = "SELECT truckId, truckNo
                  FROM truckmaster";
$selectTruckRes = mysql_query($selectTruck);
while($selectTruckRow = mysql_fetch_array($selectTruckRes))
{
  $truck['truckId'][$i] = $selectTruckRow['truckId'];
  $truck['truckNo'][$i] = $selectTruckRow['truckNo'];
  $i++;
} 
include("./bottom.php");
$smarty->assign('truck',$truck);
$smarty->assign('fromDate',$fromDate);
$smarty->assign('toDate',$toDate);
$smarty->assign('loadArray',$loadArray);
$smarty->assign('truckId',$truckId);
$smarty->display("truckTracking.tpl");
?>